using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Threading.Tasks;

public class BtnPosSizeController : MonoBehaviour
{
    public Slider h_slider, v_slider, x_slider, y_slider, size_slider;
    public Text h_text, v_text, x_text, y_text, size_text;
    public InputField UID;
    public Slider delay;
    public Text UIDText;
    public static string BgPicPath;

    public GameObject SettingPanel;
    
    float ori_X = 0, ori_Y = 0, deltaX = 0, deltaY = 0, ori_width = 0, ori_height = 0;
    float newX, newY;
    int rate = 0;
    void Start()
    {
        RectTransform tmp = GameObject.Find("13").GetComponent<RectTransform>();
        ori_X = tmp.position.x;
        ori_Y = tmp.position.y;
        ori_width = tmp.rect.x;
        ori_height = tmp.rect.y;

        tmp = GameObject.Find("14").GetComponent<RectTransform>();
        deltaX = tmp.position.x - ori_X;
        tmp = GameObject.Find("03").GetComponent<RectTransform>();
        deltaY = tmp.position.y - ori_Y;

        rate = Screen.width / 500;
        ReadData();
        OnValueChange();
        SettingPanel.SetActive(false);
    }

    public void OnValueChange()
    {
        RectTransform tmp;
        h_text.text = ((h_slider.value - 50) * rate).ToString() + "px";
        v_text.text = ((v_slider.value - 50) * rate).ToString() + "px";
        x_text.text = ((x_slider.value - 50) * rate).ToString() + "px";
        y_text.text = ((y_slider.value - 50) * rate).ToString() + "px";
        size_text.text = (size_slider.value * 0.05f + 0.5f).ToString() + "x";

        ButtonController.size_x = (10 - size_slider.value) * 0.05f * ori_width;
        ButtonController.size_y = (10 - size_slider.value) * 0.05f * ori_height;

        SaveData();
        for (int i = 0; i < 21; i++)
        {
            string name = (i / 7).ToString() + (i % 7).ToString();
            tmp = GameObject.Find(name).GetComponent<RectTransform>();

            newX = ori_X + (i % 7 - 3) * (deltaX + (h_slider.value - 50) * rate) + (x_slider.value - 50) * rate;
            newY = ori_Y + (1 - i / 7) * (deltaY + (v_slider.value - 50) * rate) + (y_slider.value - 50) * rate;
            tmp.position = new Vector3(newX, newY, 0);
            tmp.sizeDelta = new Vector2(ButtonController.size_x,ButtonController.size_y);
        }
    }

    public void SaveData()
    {
        string path = Application.persistentDataPath + "/data.txt";
        string[] data = new string[8];
        data[0] = delay.value.ToString();
        data[1] = UID.text;
        data[2] = h_slider.value.ToString();
        data[3] = v_slider.value.ToString();
        data[4] = x_slider.value.ToString();
        data[5] = y_slider.value.ToString();
        data[6] = size_slider.value.ToString();
        data[7] = BgPicPath;
        File.WriteAllLines(path, data);
    }

    public void ReadData()
    {
        string path = Application.persistentDataPath + "/data.txt";
        string[] data = { "2", "100000004", "50", "50", "50", "50", "10", "0"};
        if (File.Exists(path) == false)
        {
            File.WriteAllLines(path, data);
        }
        else
        {
            data = File.ReadAllLines(path);
        }
        delay.value = int.Parse(data[0]);
        UID.text = data[1];
        h_slider.value = int.Parse(data[2]);
        v_slider.value = int.Parse(data[3]);
        x_slider.value = int.Parse(data[4]);
        y_slider.value = int.Parse(data[5]);
        size_slider.value = int.Parse(data[6]);
        BgPicPath = data[7];
        Debug.Log(BgPicPath);
        UIDText.text = "UID: " + data[1];
    }

    public void ResetBtnPos()
    {
        h_slider.value = 50;
        v_slider.value = 50;
        x_slider.value = 50;
        y_slider.value = 50;
        size_slider.value = 10;
        OnValueChange();
    }
}
